package com.doublefish.doublefishrobot.ui.home.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.doublefish.doublefishrobot.R;

/**
 * 作者:dugaofeng on 2024/10/23 16:07
 */
public class CustomNameDialog extends Dialog implements View.OnClickListener {

    private TextView titleTxt;

    private TextView submitTxt;
    private TextView cancelTxt;
    private EditText editText;

    private Context mContext;
    private String title = null;
    private String name = null;
    private OnDialogListner listener;

    public CustomNameDialog(Context context, OnDialogListner listener) {
//        super(context,  R.style.dialog);
        super(context,  R.style.inputDialog);
        this.mContext = context;
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_mode_custom_name);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        titleTxt = findViewById(R.id.tv_title);
        if (title != null) {
            titleTxt.setText(title);
        }
        editText = findViewById(R.id.edittext);

        submitTxt = (TextView) findViewById(R.id.submit);
        submitTxt.setOnClickListener(this);

        cancelTxt = (TextView) findViewById(R.id.cancel);
        cancelTxt.setOnClickListener(this);

        //请求焦点  弹出键盘
        editText.requestFocus();
        editText.setHint(getContext().getString(R.string.input_name));
        if (name != null && !name.isEmpty()) {
            editText.setText(name);
        }
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setName(String name) {
        this.name = name;
    }

    private void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null && getCurrentFocus() != null) {
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                if (listener != null) {
                    listener.onCancelBtnClick();
                }
                hideKeyboard();
                this.dismiss();
                break;
            case R.id.submit:
                if (listener != null) {
                    listener.onComfirmBtnClick(editText.getText().toString());
                }
                hideKeyboard();
                dismiss();
                break;
        }
    }


    //region OnDialogListner

    public interface OnDialogListner {
        void onComfirmBtnClick(String text);
        void onCancelBtnClick();
    }

    //endregion

}
